package com.leyou.service.table;

import com.leyou.api.TableService;
import com.leyou.constant.DefineException;
import com.leyou.dao.ConditionMapper;
import com.leyou.dao.TableCellMapper;
import com.leyou.dao.TurnTableActivityMapper;
import com.leyou.domain.db1entity.*;
import com.leyou.domain.vo.RecordPageVO;
import com.leyou.domain.vo.TablePutVO;
import com.leyou.domain.vo.TurnTableActivityVO;
import com.leyou.domain.vo.TurnTableRecordVO;
import com.leyou.utils.Utils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author lixin
 * @Date 2018/11/13 15:13
 * @Version 1.0
 * @Description TODO
 **/
@Transactional
@Service
public class TableServiceImp implements TableService {


    @Autowired
    TurnTableActivityMapper turnTableActivityMapper;

    @Autowired
    ConditionMapper conditionMapper;

    @Autowired
    TableCellMapper tableCellMapper;


    @Override
    public void putActivity(TablePutVO tablePutVO) {
        //活动主键
        tablePutVO.setActivitykey(Utils.getUUID(32));

        //活动表
        TurnTableActivity turnTableActivity = new TurnTableActivity();
        BeanUtils.copyProperties(tablePutVO, turnTableActivity);
        int result1 = turnTableActivityMapper.insertSelective(turnTableActivity);

        //转盘条件
        Condition condition = new Condition();
        BeanUtils.copyProperties(tablePutVO.getConditionPutVO(), condition);
        condition.setActivitykey(tablePutVO.getActivitykey());
        condition.setConditionkey(Utils.getUUID(32));
        int result2 = conditionMapper.insertSelective(condition);

        //日志
        int result4 = turnTableActivityMapper.insertLog(new Log(Utils.getUUID(32), tablePutVO.getUsername(), "1", "2"));
        if (condition.getConditiontype().equals("2")) {


            List<ConditionAndGoods> conditionAndGoodsList = new ArrayList<>();
            for (ConditionAndGoods conditionAndGoods : tablePutVO.getConditionPutVO().getConditionAndGoodsList()) {
                conditionAndGoods.setConditionandgoodskey(Utils.getUUID(32));
                conditionAndGoods.setConditionkey(condition.getConditionkey());

                conditionAndGoodsList.add(conditionAndGoods);
            }
            int result5 = turnTableActivityMapper.insertConditionAndGoods(conditionAndGoodsList);

            //判断5个参数
            if (result1 == 1 && result2 == 1 && result4 == 1 &&
                    result5 == tablePutVO.getConditionPutVO().getConditionAndGoodsList().size()) {
            } else {
                throw new DefineException(400000, "插入数据不完整" + result1 + "-" + result2 + "-" + result4 + "-" + result5);
            }
        } else {
            //判断四个参数
            if (result1 == 1 && result2 == 1
                    && result4 == 1) {

            } else {
                throw new DefineException(400000, "插入数据不完整" + result1 + "-" + result2 + "-" + result4);
            }
        }
    }

    @Override
    public Map<String, Object> getTableActivityList(int pageNum, int pageSize) {

        Map<String, Object> map = new HashMap<>();

        int begin = pageSize * (pageNum - 1);

        int total = turnTableActivityMapper.selectByExample(null).size();

        List<TurnTableActivityVO> turnTableActivityList = turnTableActivityMapper.getTurnTableActivityList(begin, pageSize);

        map.put("total", total);

        map.put("turnTableActivityList", turnTableActivityList);

        return map;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void putTableCell(TableCell tableCell) {
        tableCell.setCellkey(Utils.getUUID(32));
        int result = tableCellMapper.insert(tableCell);
        if (result == 1) {
        } else {
            throw new DefineException(400000, "插入不成功");
        }

        if ("1".equals(tableCell.getCelltype())) {
            //存储优惠券规格入库
            Couponandactivity couponandactivity = new Couponandactivity();
            couponandactivity.setCouponandactivitykey(Utils.getUUID(32));
            couponandactivity.setActivitykey(tableCell.getActivitykey());
            couponandactivity.setCouponkey(tableCell.getCouponkey());
            couponandactivity.setAmount(tableCell.getQuota());
            turnTableActivityMapper.insertCouponAndActivity(couponandactivity);
        }

    }

    @Override
    public TurnTableActivity getTableById(String activityKey) {
        TurnTableActivityExample turnTableActivityExample = new TurnTableActivityExample();
        turnTableActivityExample.or().andActivitykeyEqualTo(activityKey);
        return turnTableActivityMapper.selectByExample(turnTableActivityExample).get(0);
    }

    @Override
    public void updateTableById(TurnTableActivity turnTableActivity) {
        int result = turnTableActivityMapper.updateByPrimaryKeySelective(turnTableActivity);
        if (result == 1) {
        } else {
            throw new DefineException(400000, "修改异常");
        }
    }

    @Override
    public void checkPrizeOrder(TableCell tableCell) {
        int result = turnTableActivityMapper.checkPrizeOrder(tableCell);

        if (result != 0) {
            throw new DefineException(400000, "当前序号已存在");
        }
    }

    @Override
    public List<TableCell> getTableCellList(String activitykey) {
        return tableCellMapper.getTableCellList(activitykey);
    }

    @Override
    public void deleteCell(TableCell tableCell) {
        int i = tableCellMapper.deleteCell(tableCell);
        if (i != 1) {
            throw new DefineException(400000, "删除异常");
        }
    }

    @Override
    public Map<String, Object> activityShow() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("activityShow", tableCellMapper.getActivityShow());
        map.put("recordShow", tableCellMapper.getRecordShow());
        return map;
    }

    @Override
    public Map<String, Object> getTurnTableRecord(RecordPageVO recordPageVO) {
        HashMap<String, Object> map = new HashMap<>();
        recordPageVO.setPageNum((recordPageVO.getPageNum() - 1) * recordPageVO.getPageNum());
        map.put("list", tableCellMapper.getTurnTableRecord(recordPageVO));
        map.put("total", tableCellMapper.getTurnTableRecordTotal(recordPageVO.getActivitykey()));
        return map;
    }



}
